package com.cal.calendar;

import java.util.Calendar;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

public class CalendarWidget extends Activity implements OnTouchListener {

	private static final String TAG = CalendarWidget.class.getName();

	// 视图翻页控制容器
	protected ViewFlipper viewFlipper;
	// 判断鼠标手势
	private GestureDetector detector;

	private static final int mainLayoutID = 88; // 设置主布局ID
	private static final int titleLayoutID = 77; // title布局ID
	private static final int caltitleLayoutID = 66; // title布局ID
	private static final int calLayoutID = 55; // 日历布局ID

	private RelativeLayout mainLayout;
	private Button btnToday;

	protected GridView preMonthView;// 上一个月
	protected GridView currentMonthView;// 当前月
	protected GridView latterMonthView;// 下一个月

	protected MonthGridViewAdapter preMonthViewAdapter;
	protected MonthGridViewAdapter currentMonthViewAdapter;
	protected MonthGridViewAdapter latterMonthViewAdapter;

	protected Calendar calendarStartDay = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(generateContentView());
		// UpdateStartDateForMonth();
		calendarStartDay.set(Calendar.DAY_OF_MONTH, 1);

		// 设置用户手势监听
		detector = new GestureDetector(this, new GestureListener(this));

	}

	/**
	 * 生成数据视图
	 * 
	 * @return
	 */
	private View generateContentView() {

		Log.d(TAG, "create the main context begin ... ");

		// 创建一个垂直的线性布局
		mainLayout = new RelativeLayout(this);

		RelativeLayout.LayoutParams params_main = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		mainLayout.setLayoutParams(params_main);
		mainLayout.setId(mainLayoutID);
		mainLayout.setGravity(Gravity.CENTER_HORIZONTAL);

		mainLayout.setBackgroundColor(Color.GRAY);

		Log.d(TAG, "create the main context over !");

		Log.d(TAG, "create the top year and month context begin ... ");

		// 生成顶部年月显示布局
		LinearLayout layTopControls = createLayout(LinearLayout.HORIZONTAL);

		generateTopButtons(layTopControls); // 生成顶部按钮 （上一月，下一月，当前月）
		RelativeLayout.LayoutParams params_title = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		// params_title.addRule(RelativeLayout.ALIGN_PARENT_TOP, 20);
		layTopControls.setId(titleLayoutID);

		Log.d(TAG, "create the top year and month context begin ... ");

		mainLayout.addView(layTopControls, params_title);

		Log.d(TAG, "create the viewFlipper begin ...");

		// 创建一个图册
		viewFlipper = new ViewFlipper(this);
		viewFlipper.setId(calLayoutID);

		GridView titleGridView = generateTitleGirdView();

		RelativeLayout.LayoutParams params_cal_title = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		params_cal_title.addRule(RelativeLayout.BELOW, titleLayoutID);
		// params_cal_title.topMargin = 5;
		mainLayout.addView(titleGridView, params_cal_title);

		CreateGirdView(calendarStartDay);

		RelativeLayout.LayoutParams params_cal = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		params_cal.addRule(RelativeLayout.BELOW, caltitleLayoutID);

		mainLayout.addView(viewFlipper, params_cal);

		LinearLayout br = new LinearLayout(this);

		RelativeLayout.LayoutParams params_br = new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, 1);

		params_br.addRule(RelativeLayout.BELOW, calLayoutID);
		br.setBackgroundColor(0xD8D7DC);

		mainLayout.addView(br, params_br);

		return mainLayout;

	}

	private LinearLayout createLayout(int iOrientation) {
		LinearLayout lay = new LinearLayout(this);
		LayoutParams params = new LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,// *fill_parent，填满父控件的空白
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		params.topMargin = 10;
		// 设置布局参数
		lay.setLayoutParams(params);// *wrap_content，表示大小刚好足够显示当前控件里的内容
		lay.setOrientation(iOrientation);// 设置方向
		lay.setGravity(Gravity.LEFT);
		return lay;
	}

	private void generateTopButtons(LinearLayout layTopControls) {
		// 创建一个当前月按钮（中间的按钮）
		btnToday = new Button(this);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER_VERTICAL;
		btnToday.setLayoutParams(params);
		btnToday.setTextSize(25);
		btnToday.setBackgroundColor(Color.GREEN);
		// btn_cal.setBackgroundResource(R.drawable.editbox_background_normal);//
		// 设置当前月按钮的背景颜色为按钮默认颜色

		// 当前月的点击事件的监听
		btnToday.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View arg0) {
				setToDayViewItem();
			}
		});

		layTopControls.setGravity(Gravity.CENTER_HORIZONTAL);
		layTopControls.addView(btnToday);

	}

	/**
	 * 生成顶部年月部分
	 * @return
	 */
	private GridView generateTitleGirdView() {

		GridView titleGridView = new MonthGridView(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		// params.topMargin = 5;
		titleGridView.setLayoutParams(params);
		titleGridView.setVerticalSpacing(0);// 垂直间隔
		titleGridView.setHorizontalSpacing(0);// 水平间隔

		TitleGridViewAdapter titleAdapter = new TitleGridViewAdapter(this);

		titleGridView.setAdapter(titleAdapter);// 设置菜单Adapter

		titleGridView.setId(caltitleLayoutID);

		return titleGridView;
	}

	/**
	 * 生成主要的网格部分
	 * @param calendar
	 */
	protected void CreateGirdView(Calendar calendar) {

		Calendar tempSelected = Calendar.getInstance(); // 临时
		// 获取到当前时间
		tempSelected.setTime(calendar.getTime());

		// 获取上月的时间
		tempSelected.add(Calendar.MONTH, -1);

		preMonthView = new MonthGridView(this);

		preMonthViewAdapter = new MonthGridViewAdapter(this, tempSelected);
		preMonthView.setAdapter(preMonthViewAdapter);// 设置菜单Adapter
		preMonthView.setId(calLayoutID);

		currentMonthView = new MonthGridView(this);
		// 时间加一月
		tempSelected.add(Calendar.MONTH, 1);
		currentMonthViewAdapter = new MonthGridViewAdapter(this, tempSelected);
		currentMonthView.setAdapter(currentMonthViewAdapter);// 设置菜单Adapter
		currentMonthView.setId(calLayoutID);

		latterMonthView = new MonthGridView(this);
		// 时间加一月
		tempSelected.add(Calendar.MONTH, 1);
		latterMonthViewAdapter = new MonthGridViewAdapter(this, tempSelected);
		latterMonthView.setAdapter(latterMonthViewAdapter);// 设置菜单Adapter
		latterMonthView.setId(calLayoutID);

		currentMonthView.setOnTouchListener(this);
		preMonthView.setOnTouchListener(this);
		latterMonthView.setOnTouchListener(this);

		if (viewFlipper.getChildCount() != 0) {
			viewFlipper.removeAllViews();
		}

		
		viewFlipper.addView(currentMonthView);
		viewFlipper.addView(latterMonthView);
		viewFlipper.addView(preMonthView);
		
		btnToday.setText(DateUtil.formatDate(calendar.getTime()));
	}

	// 当月
	private void setToDayViewItem() {

		btnToday.setText(DateUtil.formatDate(calendarStartDay.getTime()));

	}

	protected void setPrevViewItem() {

		calendarStartDay.set(Calendar.DAY_OF_MONTH, 1); // 设置日为当月1日
		calendarStartDay.add(Calendar.MONTH, -1); // 设置月

	}

	protected void setNextViewItem() {

		calendarStartDay.set(Calendar.DAY_OF_MONTH, 1); // 设置日为当月1日
		calendarStartDay.add(Calendar.MONTH, 1); // 设置月
	}

	/**
	 * 分析是否有合适的 callback函数来处理用户的手势
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {

		return detector.onTouchEvent(event);
	}
}
